import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Demo30 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //定义任务
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int sum = 0;
                for(int i = 0; i <= 1000; i++)
                {
                    sum += i;
                }
                return sum;
            }
        };

        //把任务放在线程中
        FutureTask<Integer> futureTask = new FutureTask<>(callable);
        Thread t = new Thread(futureTask);
        t.start();

        //此处的get就能获取到callabel里的返回结果
        //由于线程是并发执行的, 执行到主线程的get的时候, t线程可能还没执行完
        //没执行完, get就会阻塞

        System.out.println(futureTask.get());
    }
}
